<%@ page import="cn.manager.travel.domain.New" %>
<%@ page import="java.util.List" %>
<%@ page import="cn.manager.travel.service.AdminService" %>
<%@ page import="cn.manager.travel.service.impl.AdminServiceImpl" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<%
    if (request.getSession().getAttribute("user") == null) {
    	request.setAttribute("error","您尚未登录,请登录后在使用其他功能");
    	request.getRequestDispatcher("login.jsp").forward(request,response);
    }
%>
<head>
    <title>后台管理</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <script src="./js/vendor/jquery-3.6.0.min.js"></script>
    <script src="./js/popper.min.js"></script>
    <script src="./js/bootstrap.min.js"></script>
    <style>
        .box{
            max-height: 725px;
            overflow-y: scroll;
        }
    </style>
</head>
<body>

<nav class="navbar navbar-expand-sm bg-light">
    <div class="container">
        <div class="row col-12">
            <div class="col-6">
                <a href="${pageContext.request.contextPath}/index.jsp">首页</a>
            </div>
            <div class="col-6 text-right">
                <span>${user.username}</span>
                <a href="" id="quit" onclick="quit()">退出登录</a>
            </div>
        </div>
    </div>
</nav>
<br>

<div class="box">
    <div class="container-fluid">
        <div class="title mb-2">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
                添加
            </button>
        </div>
        <table class="col-12 text-center table table-striped">
            <thead>
            <tr>
                <th>新闻ID</th>
                <th>标题</th>
                <th>作者</th>
                <th>点击数量</th>
                <th>内容</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <%
                AdminService service = new AdminServiceImpl();
                List<New> news = service.showNews();
                request.setAttribute("news", news);
            %>
            <c:forEach items="${news}" var="item">
                <tr>
                    <td>${item.nID}</td>
                    <td>${item.title}</td>
                    <td>${item.author}</td>
                    <td>${item.click_count}</td>
                    <td>${item.text}</td>
                    <td>${item.date}</td>
                    <td>
                        <button data-id="${item.nID}" type="button" class="change btn btn-info" data-toggle="modal" data-target="#myModal2">
                            修改
                        </button>
                        <button class="btn del btn-primary" onclick="delNews(${item.nID})">删除</button>
                    </td>
                </tr>
            </c:forEach>

            </tbody>
        </table>
    </div>
</div>
<!-- 添加 -->
<div class="container">
    <!-- 模态框 -->
    <div class="modal fade" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- 模态框头部 -->
                <div class="modal-header">
                    <h4 class="modal-title">添加</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- 模态框主体 -->
                <div class="modal-body">
                    <form id="add_form" action="${pageContext.request.contextPath}/admin/add">
                        <div class="form-group">
                            <label >标题</label>
                            <input required type="text" id='add_title' name="title" class="form-control">
                        </div>
                        <div class="form-group">
                            <label > 作者 </label>
                            <input required type="text" id="add_author" name="author" class="form-control">
                        </div>
                        <div class="form-group">
                            <label >内容</label>
                            <textarea required class="form-control" id="add_text" name="text" cols="30" rows="10"></textarea>
                        </div>
                        <div class="form-group">
                            <button class="btn col-12 btn-primary">提交</button>
                        </div>
                    </form>
                </div>
                <!-- 模态框底部 -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                </div>

            </div>
        </div>
    </div>

</div>

<!-- 修改 -->
<div class="container">
    <!-- 模态框 -->
    <div class="modal fade" id="myModal2">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- 模态框头部 -->
                <div class="modal-header">
                    <h4 class="modal-title">修改</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- 模态框主体 -->
                <div class="modal-body">
                    <form id="change_form" action="admin/upd">
                        <div class="form-group">
                            <input type="hidden" id="change_id" name="nID">
                        </div>
                        <div class="form-group">
                            <label >标题</label>
                            <input type="text" id='change_title' name="title" class="form-control">
                        </div>
                        <div class="form-group">
                            <label > 作者 </label>
                            <input type="text" id="change_author" name="author" class="form-control">
                        </div>
                        <div class="form-group">
                            <label >内容</label>
                            <textarea class="form-control" id="change_text" name="text" cols="30" rows="10"></textarea>
                        </div>
                        <div class="form-group">
                            <button class="btn col-12 btn-primary">提交</button>
                        </div>
                    </form>
                </div>

                <!-- 模态框底部 -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                </div>

            </div>
        </div>
    </div>

</div>
<script>

    function quit(){
        location.href = '${pageContext.request.contextPath}/admin/quit';
    }

    function delNews(nId){
        if (confirm("你确定要删除此记录吗?")){
            location.href="${pageContext.request.contextPath}/admin/del?nId="+nId;
        }
    }
    let news = null
    getNews()
    async function getNews(){
        return new Promise((resolve,reject)=>{
            $.post('admin/getNews','',function (data){
                resolve(data)
            })
        })
    }

    $('.change').on('click',async function (){
        let data = await getNews()
        let id = $(this).attr('data-id')
        let res = data.filter(ele=>ele.nID == id)[0]
        console.log(res)
        change_id.value = res.nID;
        change_title.value =  res.title
        change_author.value =  res.author
        change_text.value =  res.text
    })
</script>
</body>

</html>